package com.yuanfeng.job.service.impl;


import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.rocketmq.MqSendUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.job.mapper.UpdateCommonStatuJobMapper;
import com.yuanfeng.job.service.UpdateCommonStatuJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Stranger
 * @date 2023/5/17 14:52
 */
@Service
public class UpdateCommonStatuJobServiceImpl implements UpdateCommonStatuJobService {

    @Autowired
    private UpdateCommonStatuJobMapper updateCommonStatuJobMapper;
    @Override
    public ResponseResult queryGoodsIdByState() {
        // common表goodsId
        List<Map<String,Object>> goodsIds = updateCommonStatuJobMapper.queryGoodsIdByState();

        // goods表goodsId
        List<Map<String,Object>> goodsIdsBase = updateCommonStatuJobMapper.queryGoodsIdByBase();

        Map<String,Object> map= new HashMap<>();
        List<Integer> goodIds = new ArrayList<>();

        for (Map<String,Object> goodsId : goodsIds) {
            if ("0".equals(goodsId.get("isEnquiry").toString())) {
                updateCommonStatuJobMapper.updateCommonStatu(goodsId.get("goodsId").toString());
                updateCommonStatuJobMapper.updateGoodsStatu(goodsId.get("goodsId").toString());
            }
        }
        for (Map<String,Object> goodsId : goodsIdsBase) {
            if ("0".equals(goodsId.get("isEnquiry").toString())) {
                updateCommonStatuJobMapper.updateGoodsStatu(goodsId.get("goodsId").toString());
                goodIds.add(Integer.parseInt(goodsId.get("goodsId").toString()));
            }
        }
        //通过mq 更新es的商品数据
        map.put("goodsIdS",goodIds);
        map.put("type",2);// 1 是添加  2是删除
        MqSendUtils.mqSendGoods(map);

        return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL.getMsg());
    }
}
